#ifndef CDISPLAYWIDGET_H
#define CDISPLAYWIDGET_H

#include <QObject>
#include <QWidget>

#include <QPaintEvent>
#include <QMouseEvent>
#include <QTimer>

#include "CGridMap.h"
#include "RRT_Type.h"

class CDisplayWidget : public QWidget
{
    Q_OBJECT
public:
    explicit CDisplayWidget(QWidget *parent = nullptr);

private:

    enum TMouseEvent
    {
        MOUSE_EVENT_NULL = 0,
        MOUSE_EVENT_SET_OBSTACLE,
        MOUSE_EVENT_SET_START_POINT,
        MOUSE_EVENT_SET_DEST_POINT,
    };

    void initWidget();

    void expandClickObstaclePoint(QPoint click_pos, QVector<QPoint> &point_list);

private:
    void paintEvent(QPaintEvent *);

    void mousePressEvent(QMouseEvent *event);

    void paintGrid(QPainter &painter);

    void paintObstacle(QPainter &painter);

    void paintStartAndDestPoint(QPainter &painter);

    void paintRandomNewPoint(QPainter &painter);

    void paintTreePoint(QPainter &painter);

    void paintOriginPathPoint(QPainter &painter);

    void paintCar(QPainter &painter);

    template<class T>
    void clearVector(QVector<T> &t)
    {
        QVector<T> tmp;
        tmp.swap(t);
    }

public slots:

    void slotInitMode(int mouse_event, int obstacle_size);

    void slotResetMap();

    void slotClearPath();

    void slotDisplayRandomPoint(QPoint point);

    void slotReceiveNewTreePoint(TNode new_node);

    void slotReceiveOriginPath(QVector<QPoint> path);

private slots:

    void initTimerCallback();

signals:

    void signalNotifyMapSize(quint16 height, quint16 width);

    void signalUpdateMapData(QVector<QPoint> data);

    void signalSetStartPoint(QPoint);
    void signalSetDestPoint(QPoint);

private:

    quint16 m_size_x;
    quint16 m_size_y;

    TMouseEvent  m_mouse_event;
    int          m_obstacle_expand_size;
    QTimer * m_init_timer = nullptr;

    QVector<QPoint>  m_obstacle_list;
    QPoint           m_start_point;
    QPoint           m_dest_point;
    QPoint           m_new_random_point;
    QVector<TNode>   m_tree_point_list;
    QVector<QPoint>  m_origin_path;


};

#endif // CDISPLAYWIDGET_H
